<?php
if($_SESSION['group_id']!=1){redir("./");exit;}

if($_GET['op']==''){
?>
<div class="box">
  <h3>รายชื่อสาขา [<a href="?mod=a.addbranch">เพิ่ม</a>]</h3>
  <div>
  	<form method="get">
		<input name="search" value="<?php echo $search;?>" />
		<input type="hidden" name="mod" value="<?php echo $mod;?>" />
		<input type="submit" value="ค้นหา" />
	</form>
  </div>
  <div class="boxContent">
  <?php
  $where=" where branch_name like '%$search%'";
  $sql="select * from branch $where order by dep_id,branch_name asc";
  $res=mysql_query($sql) or die(mysql_error());
  $page=$_GET['page'];
  if(!$page)$page=1;
  $allrows=mysql_num_rows($res);
  $allpage=ceil($allrows/$cfg_limit);
  $limitstart=($page-1)*$cfg_limit;
  $sql.=" limit $limitstart,$cfg_limit";
  $res=mysql_query($sql);
  $pageurl="?mod=$mod&search=$search";
  if($allrows>0){
  ?>
    <form method="post">
	<table width="100%">
      <tr>
        <td width="10"><strong>#</strong></td>
        <td><strong>ชื่อสาขา</strong></td>
        <td><strong>ภาควิชา</strong></td>
        <td colspan="2" align="center"><strong>ดำเนินการ</strong></td>
	  </tr>
	  <?php
	  	$res=mysql_query($sql);
		$i=0;
	  	while($row=mysql_fetch_assoc($res)){
			$dep=mysql_fetch_assoc(mysql_query("select * from department where dep_id='$row[dep_id]'"));
			++$i;
	  		$class=($i%2!=0)?'rowA':'rowB';
	  ?>
      <tr class="<?php echo $class;?>">
        <td width="10"><?php echo $i;?></td>
        <td><?php echo $row['branch_name'];?></td>
        <td><?php echo $dep['dep_name'];?></td>
        <td align="center"><?php echo "<a href=?mod=$mod&op=edit&branch_id=$row[branch_id]>แก้ไข</a>";?></td>
        <td align="center"><?php echo "<a href=?mod=$mod&op=del&branch_id=$row[branch_id] onclick=\"return confirm('ยืนยันการลบ');\">ลบ</a>";?></td>
        </tr>
	  <?php
	  }
	  ?>
    </table>
	</form>
  <?php
  	pagemenu($pageurl,$page,$allpage);
  }else{
  	echo "<div id=error_msg>ไม่พบข้อมูล</div>";
  }
  ?>
  </div>
</div>
<?php
}else if($_GET['op']=='edit'){
	if($_POST['op']=='update'){
		$dep_id=$_POST['dep_id'];
		$branch_name=$_POST['branch_name'];
		if(!$branch_name)$err="กรุณากรอก ชื่อสาขา";
		else{
			$sql="update branch set branch_name='$branch_name',dep_id='$dep_id' where branch_id='$_GET[branch_id]'";
			mysql_query($sql);
			redir("?mod=$mod",5);
			die("<div id=com_msg>ปรับปรุงข้อมูลเรียบร้อยแล้วกรุณารอสักครู่ <img src=images/load.gif></div>");
		}
	}
	$row=mysql_fetch_assoc(mysql_query("select * from branch where branch_id='$_GET[branch_id]'"));
?>
<div class="box">
  <h2>แก้ไขข้อมูลสาขา</h2>
  <?php if($err)echo "<div id=error_msg>$err</div>";?>
  <div class="boxContent">
    <p> </p>
    <form method="post" enctype="multipart/form-data">
      <table width="99%" border="0" cellspacing="2" cellpadding="2">
        <tr>
          <td width="120"><strong>ชื่อสาขา</strong></td>
          <td>
		  <select name="dep_id">
		  <?php
		  	$sql="select * from department order by dep_id";
		  	$res=mysql_query($sql);
		 	while($dep=mysql_fetch_assoc($res)){
		 		$select=($row['dep_id']==$dep['dep_id'])?'selected':'';
		 		echo "<option value='$dep[dep_id]' $select>$dep[dep_name]</option>";
		  	}
		  	?>
		  </select>
		  </td>
        </tr>
		<tr>
          <td width="120"><strong>ชื่อสาขา</strong></td>
          <td><input name="branch_name" type="text" id="branch_name" value="<?php echo $row['branch_name'];?>" /></td>
        </tr>
      </table>
      <table width="99%" border="0" cellspacing="2" cellpadding="2">
        <tr>
          <td align="center"><input type="submit" class="button" name="Submit" value="ปรับปรุงข้อมูล" />
              <input name="op" type="hidden" id="op" value="update" /></td>
        </tr>
      </table>
    </form>
    </p>
  </div>
</div>
<?php
}else if($_GET['op']=='del'){
	mysql_query("delete from branch where branch_id='$_GET[branch_id]'");
	redir("?mod=$mod&".time());exit;
}
?>